import {Toast, TextareaField} from 'saltui';
import {Icon} from 'salt-icon';

const {zllxdata, aqlxdata, ysjd} = require('./../../contas/contas.js');
const {fromJS} = require('immutable');

module.exports = {

  initYsjd: function (jdjl, ysjd) {
    let tmp = jdjl.get('ysjd').split(',');

    tmp.map((v, k) => {
      ysjd.map((v2, k2) => {
        if (v == v2.text) {
          v2.checked = true;
        }
      })
    });
    return ysjd;
  },

  initXxjd: function (jdjl, xxjd) {
    let tmp = jdjl.get('xxjdms').split(',');
    tmp.map((v, k) => {
      xxjd.map((v2, k2) => {
        if (v == v2.text) {
          v2.checked = true;
        }
      })
    });
    return xxjd;
  },
  initXxjda: function (jdjl, xxjd) {
    let tmp = jdjl.get('xxjd');
    let xxjda = {};
    xxjd.map((v2, k2) => {

      if (tmp == v2.text) {
        xxjda = v2;
        console.info('xxjda', xxjda);
      }
    })

    return xxjda;
  },
  ysjdChange: function (jdjl, params) {

    let ysjd = [];
    params.map((v, k) => {
      ysjd.push(v.text);
    });
    jdjl = jdjl.set('ysjd', ysjd.join(','));
    return jdjl;

  },

  xxjdChange: function (jdjl, params) {
    let xxjdms = [];
    params.map((v, k) => {
      xxjdms.push(v.text);
    });
    jdjl = jdjl.set('xxjdms', xxjdms.join(','));
    return jdjl;

  },

  xxjdAChange: function (jdjl, params) {
    console.info('paramsxxjdAChange', params);

    let xxjd = [];
    params.map((v, k) => {
      xxjd.push(v.text);
    });
    jdjl = jdjl.set('xxjd', xxjd.join(','));
    return jdjl;
  },

  //选择工程
  engSelect: function (jdjl, params) {
    console.info('eng', params);
    jdjl = jdjl.set('eng', fromJS(params));
    return jdjl;
  },

  zrztSelect: function (jdjl, zrzt, params) {
    zrzt.map((v, k) => {
      params.map((v1, k1) => {

        if (v1.value = v.value) {
          v = v1;
        }
      })
    });
    jdjl = jdjl.set('cjjgs', fromJS(zrzt));
    return jdjl;
  },

  zrztNameSelect: function (jdjl, zrzt, params, value) {

    zrzt.map((v, k) => {
      if (params === v.jglx) {
        v.value = value;
        v.orgName = value;
      }
    });
    jdjl = jdjl.set('cjjgs', fromJS(zrzt));
    return jdjl;
  },

  //选择问题类型
  wtlxSelect: function (jdjl, params) {

    let tmpZllxdata = fromJS(zllxdata);
    params.wtlx.map((v2, k2) => {

      tmpZllxdata.map((v, k) => {
        if (v2.text == v.get('text')) {
          tmpZllxdata = tmpZllxdata.set(k, fromJS(v2));
        }
      })
    });
    jdjl = jdjl.set('wt', jdjl.get('wt').set(params.index, jdjl.get('wt').get(params.index).set('wtlx', tmpZllxdata)));
    return jdjl;

  },

  //选择问题类型
  wtlxSelectForLX: function (jdjl, params, lxdata) {

    let tmplxdata = fromJS(lxdata);
    params.wtlx.map((v2, k2) => {

      tmplxdata.map((v, k) => {
        if (v2.text == v.get('text')) {
          tmplxdata = tmplxdata.set(k, fromJS(v2));
        }
      })
    });
    jdjl = jdjl.set('wt', jdjl.get('wt').set(params.index, jdjl.get('wt').get(params.index).set('wtlx', tmplxdata)));
    return jdjl;

  },

  //添加问题描述
  wtmsChange: function (jdjl, params) {
    jdjl = jdjl.set('wt', jdjl.get('wt').set(params.index, jdjl.get('wt').get(params.index).set('wtms', params.wtms)));
    return jdjl;
  },

  //添加质量问题
  wtAdd: function (jdjl) {
    jdjl = jdjl.set('wt', jdjl.get('wt').push(fromJS({
      //问题类型
      wtlx: zllxdata,
      //问题描述
      wtms: '',
      //图片
      images: [],
      //文件
      files: [],
      //创建用户
      creatorId: '',
      //排序
      sort: jdjl.get('wt').count() + 1,
    })));
    return jdjl;
  },

  //添加安全问题
  wtAqAdd: function (jdjl) {
    jdjl = jdjl.set('wt', jdjl.get('wt').push(fromJS({
      //问题类型
      wtlx: aqlxdata,
      //问题描述
      wtms: '',
      //图片
      images: [],
      //文件
      files: [],
      //创建用户
      creatorId: '',
      //排序
      sort: jdjl.get('wt').count() + 1,
    })));
    return jdjl;
  },

  //删除问题
  wtDel: function (jdjl, params) {
    jdjl = jdjl.set('wt', jdjl.get('wt').delete(params));
    return jdjl;
  },

  initZrzt: function (jdjl, zrzt, readOnly) {
    zrzt = [];
    let tmp = jdjl.get('cjjgs');
    tmp.map((v, k) => {
      /*console.info('参建单位', fromJS(v.get('orgName')))*/
      zrzt.push(
        {
          disable: readOnly,
          checked: v.get('checked'),
          jglx: v.get('jglx'),
          text: <p style={{wordBreak: 'normal', wordBreakWrap: 'break-word'}}>
            {v.get('jglx')}: {v.get('orgName')}</p>,
          value: v.get('orgName'),
          sort: v.get('sort'),
          engOrgId: v.get('engOrgId'),
          orgName: v.get('orgName'),
        }
      )
    });
    return zrzt;
  },

  //IOS时间只支持2017/01/01 所以需要转换格式
  getDateDiff: function (startDiffTime) {
    //将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式
    let startTime;
    if (startDiffTime) {
      startTime = startDiffTime.replace(/\-/g, "/");
    }

    return startTime;
  },

  //获取监督人员
  getJdry: function (jdjl, t,jllx) {
    let tmpRy  = jdjl.getIn(['eng', 'jdry']).toJS();
    let jdry = [];

    tmpRy.map((v, k) => {
      // console.info('人员',fromJS(v.ryName));
      // console.info('人员',v.ryGw);
      // console.info('人员',v.empId);
      if(v.ryGw.indexOf(jllx)>-1)
      {
        jdry.push({
          emplId: v.empId,
          avatar: '',
          jdgw:v.ryGw,
          name: v.ryName,
        });
      }
    });
    return jdry;
  },

  //获取责任主体
  getZrzt: function (jdjl, t) {

    let zrzt = [];
    zrzt.push(
      {
        checked: false,
        sort: 1,
        jglx: '建设单位',
        text: <TextareaField label="建设单位" minRows={1} maxRows={2}
                             value={jdjl.getIn(['eng', 'engJsdw'])} readOnly={true}/>
        ,
        value: jdjl.getIn(['eng', 'engJsdw']),
        engOrgId: jdjl.getIn(['eng', 'engJsdwId']),
        orgName: jdjl.getIn(['eng', 'engJsdw']),
        slotText: '建设单位: ' + jdjl.getIn(['eng', 'engJsdw']),
      }
    );
    zrzt.push(
      {
        checked: false,
        sort: 2,
        jglx: '施工单位',
        text: <TextareaField label="施工单位" minRows={1} maxRows={2}
                             value={jdjl.getIn(['eng', 'engSgdw'])} readOnly={true}/>
        ,
        value: jdjl.getIn(['eng', 'engSgdw']),
        engOrgId: jdjl.getIn(['eng', 'engSgdwId']),
        orgName: jdjl.getIn(['eng', 'engSgdw']),
        slotText: '施工单位: ' + jdjl.getIn(['eng', 'engJsdw']),
      }
    );
    zrzt.push(
      {

        checked: false,
        sort: 3,
        jglx: '监理单位',
        text: <TextareaField label="监理单位" minRows={1} maxRows={2}
                             value={jdjl.getIn(['eng', 'engJldw'])} readOnly={true}/>
        ,
        value: jdjl.getIn(['eng', 'engJldw']),
        engOrgId: jdjl.getIn(['eng', 'engJldwId']),
        orgName: jdjl.getIn(['eng', 'engJldw']),
        slotText: '监理单位: ' + jdjl.getIn(['eng', 'engJldw']),
      }
    );
    zrzt.push(
      {

        checked: false,
        sort: 4,
        jglx: '勘察单位',
        text: <TextareaField label="勘察单位" minRows={1} maxRows={2}
                             value={jdjl.getIn(['eng', 'engKcdw'])} readOnly={true}/>
        ,
        value: jdjl.getIn(['eng', 'engKcdw']),
        engOrgId: jdjl.getIn(['eng', 'engKcdwId']),
        orgName: jdjl.getIn(['eng', 'engKcdw']),
        slotText: '勘察单位: ' + jdjl.getIn(['eng', 'engKcdw']),
      }
    );
    zrzt.push(
      {
        checked: false,
        sort: 5,
        jglx: '设计单位',
        text: <TextareaField label="设计单位" readOnly={true} minRows={1} maxRows={2}
                             value={jdjl.getIn(['eng', 'engSjdw'])}/>,
        value: jdjl.getIn(['eng', 'engSjdw']),
        engOrgId: jdjl.getIn(['eng', 'engSjdwId']),
        orgName: jdjl.getIn(['eng', 'engSjdw']),
        slotText: '设计单位: ' + jdjl.getIn(['eng', 'engSjdw']),
      }
    );
    zrzt.push(
      {
        checked: false,
        sort: 6,
        jglx: '其他单位',
        text: <TextareaField label="其他单位" minRows={1} maxRows={2} onChange={e => {
          t.onZrztNameChange(e, '其他单位')
        }}
                             placeholder={'请输入文字'} value={jdjl.getIn(['eng', 'engQtdw'])}/>,
        value: jdjl.getIn(['eng', 'engQtdw']),
        engOrgId: jdjl.getIn(['eng', 'engQtdwId']),
        orgName: jdjl.getIn(['eng', 'engQtdw']),
        slotText: '其他单位: ' + jdjl.getIn(['eng', 'engQtdw']),
      }
    );
    return zrzt;
  },

  //删除附件
  fileDel: function (jdjl, params) {
    let $$file1 = jdjl.get('wt').get(params.index).get('files').delete(params.fileIndex);
    let $$file2 = jdjl.get('wt').get(params.index).set('files', $$file1);
    let $$file3 = jdjl.get('wt').set(params.index, $$file2);
    jdjl = jdjl.set('wt', $$file3);
    return jdjl;
  },


  //添加附件
  fileAdd: function (jdjl, params) {
    params.files.map((item, index) => {
      let $$file1 = jdjl.get('wt').get(params.index).get('files').push(fromJS(item));
      let $$file2 = jdjl.get('wt').get(params.index).set('files', $$file1);
      let $$file3 = jdjl.get('wt').set(params.index, $$file2);
      jdjl = jdjl.set('wt', $$file3);

    });
    return jdjl;
  },

  //添加上传图片
  imageAdd: function (jdjl, params) {
    params.images.map((item, index) => {
      let $$img1 = jdjl.get('wt').get(params.index).get('images').push(fromJS({
        imgUrl: item,
        sort: jdjl.get('wt').get(params.index).get('images').count() + 1,
      }));
      let $$img2 = jdjl.get('wt').get(params.index).set('images', $$img1);
      let $$img3 = jdjl.get('wt').set(params.index, $$img2);
      jdjl = jdjl.set('wt', $$img3);
    });

    return jdjl;
  },


  //删除图片
  imageDel: function (jdjl, params) {
    let $$img1 = jdjl.get('wt').get(params.index).get('images').delete(params.imageIndex);
    let $$img2 = jdjl.get('wt').get(params.index).set('images', $$img1);
    let $$img3 = jdjl.get('wt').set(params.index, $$img2);
    jdjl = jdjl.set('wt', $$img3);
    return jdjl;
  },

  //地图定位
  mapEdit: function (jdjl, params) {
    jdjl = jdjl.set('map', fromJS(params));
    return jdjl;
  },

  //删除监督人员
  jdryDel: function (jdjl, params) {
    jdjl = jdjl.set('jdrys', jdjl.get('jdrys').delete(params));
    return jdjl;
  },

  //检查监督人员
  jdryCheck: function (jdry, params) {
    let bz = false;
    if(null !== jdry)
    {
      jdry.forEach((item, index) => {
        if(item.name===params)
        {
          bz = true;
        }
      });
    }
    return bz;
  },

  //添加监督人员
  jdryAdd: function (jdjl, params) {
    jdjl = jdjl.set('jdrys', jdjl.get('jdrys').push(fromJS(params)));
    return jdjl;
  },

  //只有新增操作才会更改STORAGE的值
  saveLocalStorage: function (operType, key, jdjl, storage) {
    if (operType == 'add') {
      storage.set(key, jdjl.toJS());
    }
  },
  //弹出对话框
  showTost: function (type, content, autoHide, hasMask) {
    Toast.show({
      type: type,
      content: content,
      autoHide: autoHide,
      hasMask: hasMask,
    });
  },

};
